package app.modules.example.application.web;

import app.modules.common.dto.web.WebBatchIdsDTO;
import app.modules.common.dto.web.WebIdDTO;
import app.modules.common.util.web.PageUtil;
import app.modules.example.business.service.ExampleService;
import app.modules.example.constant.ExampleErrorConstants;
import app.modules.example.dto.web.WebExampleCheckDTO;
import app.modules.example.dto.web.WebExampleDTO;
import app.modules.example.dto.web.WebExampleQueryDTO;
import app.modules.example.dto.web.WebExampleSaveDTO;
import app.modules.example.dto.web.WebExampleUpdateDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jasmine.framework.web.entity.WebResult;
import jasmine.framework.web.util.validation.ValidationHelper;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author mh.z
 */
@Tag(name = "示例API")
@RestController
public class ExampleController {
    private final ExampleService exampleService;

    public ExampleController(ExampleService exampleService) {
        this.exampleService = exampleService;
    }

    @Operation(summary = "检查数据")
    @PostMapping("/api/example/checkData")
    public ResponseEntity<WebResult> checkData(@RequestBody WebExampleCheckDTO dto) {
        boolean checkResult = exampleService.checkData(dto);

        WebResult result = checkResult
                ? WebResult.success()
                : WebResult.error(ExampleErrorConstants.CHECK_FAILED, null);

        return result.toOkEntity();
    }

    @Operation(summary = "分页查询数据")
    @PostMapping("/api/example/pageData")
    public ResponseEntity<WebResult<List<WebExampleDTO>>> pageData(@RequestBody WebExampleQueryDTO queryDTO) {
        Page page = PageUtil.getPage(queryDTO);

        List<WebExampleDTO> recordList = exampleService.pageWebExampleDTOsByCond(queryDTO, page);
        WebResult result = PageUtil.getResult(recordList, page);

        return result.toOkEntity();
    }

    @Operation(summary = "删除指定 ID 的记录")
    @PostMapping("/api/example/deleteRecordById")
    public ResponseEntity<WebResult<Object>> deleteRecordById(@RequestBody WebIdDTO paramDTO) {
        Long recordId = paramDTO.getId();
        exampleService.deleteRecordById(recordId);

        return WebResult.success().toOkEntity();
    }

    @Operation(summary = "批量删除指定 ID 的记录")
    @PostMapping("/api/example/batchDeleteRecordByIds")
    public ResponseEntity<WebResult<Object>> deleteRecordById(@RequestBody WebBatchIdsDTO paramDTO) {
        List<Long> recordIdList = paramDTO.getIds();
        exampleService.deleteRecordByIds(recordIdList);

        return WebResult.success().toOkEntity();
    }

    @Operation(summary = "查看详情")
    @PostMapping("/api/example/getDetail")
    public ResponseEntity<WebResult<WebExampleDTO>> getDetail(@RequestBody WebIdDTO dto) {
        Long id = dto.getId();
        WebExampleDTO exampleDTO = exampleService.getWebExampleDtoById(id);

        return WebResult.success(exampleDTO).toOkEntity();
    }

    @Operation(summary = "修改详情")
    @PostMapping("/api/example/updateDetail")
    public ResponseEntity<WebResult<WebExampleDTO>> updateDetail(@Valid @RequestBody WebExampleUpdateDTO dto, Errors errors) {
        ValidationHelper helper = ValidationHelper.create(errors);
        helper.field("exampleName").rejectIfEmpty();

        if (helper.hasErrors()) {
            return helper.toOkEntity();
        }

        WebExampleDTO exampleDTO = exampleService.updateExample(dto);

        return WebResult.success(exampleDTO).toOkEntity();
    }

    @Operation(summary = "保存详情")
    @PostMapping("/api/example/saveDetail")
    public ResponseEntity<WebResult<WebExampleDTO>> saveDetail(@Valid @RequestBody WebExampleSaveDTO dto, Errors errors) {
        ValidationHelper helper = ValidationHelper.create(errors);
        helper.field("exampleName").rejectIfEmpty();

        if (helper.hasErrors()) {
            return helper.toOkEntity();
        }

        WebExampleDTO exampleDTO = exampleService.saveExample(dto);

        return WebResult.success(exampleDTO).toOkEntity();
    }

}
